#include "include/low_power.h"
#include "include/lp_gsensor.h"
#include "task/gsensor/gsensor.h"

#include "bsp.h"

#define LOG_TAG TAG_LP
#include "elog.h"

#include "drivers/sensor/skeleton/sensor_support_chip.h"
#include "gsensor/gsensor.h"

/**
 * @brief 休眠使能检测
 * @return 0 - 不能休眠, TASK_ALWAYS_SLEEP - 一直休眠, 其他数值: 可休眠的时间, unit: ms
 */
uint32_t gsensor_canstop(void)
{
    if(gsensor_unusal_move_check())
        return 0;
    
    return (24*3600*1000);
}

/**
 * @brief 休眠前处理函数
 */
void gsensor_prestop(void)
{
    /* Gsensor进入休眠前清除中断标记, 避免中断触发期间进入休眠无法唤醒 */
    clear_wom_int_flags();
}

/**
 * @brief 休眠唤醒后处理函数
 */
void gsensor_poststop(void)
{
    /* 骑行状态下，需要正常模式检测gyro */
    if(get_gs_collide_machine_state() == GSENSOR_NORMAL_WORK) {

        /* 设置GSENSOR进入正常工作模式 */
        icm42670_sensor_initialize();
    }

    /* 设置INT1中断 */
    //gsensor_int1_enable(NULL, NULL);
}

// 依赖工作模式和激活状态进行配置
sleep_item_t lp_gsensor = {
    .name       = "GSENSOR", 
    .canstop    = gsensor_canstop,
    .prestop    = gsensor_prestop,
    .poststop   = gsensor_poststop,
};